package cn.itcast.itcaststore.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/ChangeCardServlet")
public class ChangeCardServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        int index = Integer.parseInt(request.getParameter("index"));
        int count = Integer.parseInt(request.getParameter("count"));

        ArrayList card = (ArrayList) session.getAttribute("card");
        ArrayList list = (ArrayList) card.get(index-1);
        list.set(1, count);

        session.setAttribute("card",card );
        response.sendRedirect(request.getContextPath()+"/client/cart.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
